﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace Sinacor.Infra.UI.Messaging.SmartClient.WeakEventManagers
{

    internal class OnErrorDeliveringInternalMessageWeakEventArgs : EventArgs
    {
        private Exception _ex;
        public Exception Ex
        {
            get { return _ex; }
            set { _ex = value; }
        }
        
        private Internal.InternalMessage _internalMessage;
        public Internal.InternalMessage InternalMessage
        {
            get { return _internalMessage; }
            set { _internalMessage = value; }
        }

        public OnErrorDeliveringInternalMessageWeakEventArgs(Exception ex, Internal.InternalMessage internalMessage)
        {
            _ex = ex;
            _internalMessage = internalMessage;
        }
    }

    internal class OnErrorDeliveringInternalMessageManager : WeakEventManager
    {
        private object _source;
        protected override void StartListening(object source)
        {
            MessageManager c = source as MessageManager;
            if (c != null)
            {
                c.OnErrorDeliveringInternalMessage += new MessageManager.ErrorDeliveringInternalMessage(OnErrorDeliveringInternalMessage);
                _source = source;
            }
        }

        protected override void StopListening(object source)
        {
            MessageManager c = source as MessageManager;
            if (c != null)
                c.OnErrorDeliveringInternalMessage -= new MessageManager.ErrorDeliveringInternalMessage(OnErrorDeliveringInternalMessage);

            _source = null;
        }

        void OnErrorDeliveringInternalMessage(Exception e, Sinacor.Infra.UI.Messaging.Internal.InternalMessage internalMessage)
        {
            base.DeliverEvent(_source, new OnErrorDeliveringInternalMessageWeakEventArgs(e, internalMessage));
        }

        public static void AddListener(object source, IWeakEventListener listener)
        {
            CurrentManager.ProtectedAddListener(source, listener);
        }

        public static void RemoveListener(object source, IWeakEventListener listener)
        {
            CurrentManager.ProtectedRemoveListener(source, listener);
        }

        private static OnErrorDeliveringInternalMessageManager CurrentManager
        {
            get
            {
                Type managerType = typeof(OnErrorDeliveringInternalMessageManager);
                OnErrorDeliveringInternalMessageManager currentManager = (OnErrorDeliveringInternalMessageManager)WeakEventManager.GetCurrentManager(managerType);
                if (currentManager == null)
                {
                    currentManager = new OnErrorDeliveringInternalMessageManager();
                    WeakEventManager.SetCurrentManager(managerType, currentManager);
                }
                return currentManager;
            }
        }
    }
}
